package com.zx.mes.rocketmq.hyl.quickstart;

import com.zx.mes.rocketmq.hyl.constant.Const;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

/**
 * quick start test
 *
 * @author 华云龙
 * @date 2018-11-27
 */
public class Producer {

    private static final int INT = 5;
    private static final String TEST_QUICK_TOPIC = "test_quick_topic";

    public static void send() throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        String producerGroup = "test_quick_producer_name";
        DefaultMQProducer producer = new DefaultMQProducer(producerGroup);
        producer.setNamesrvAddr(Const.NAMESRV_ADDR_SINGLE);
        /// 会报MQClientException异常
        producer.start();

        /// 创建消息,并发送
        for (int i = 0; i < INT; i++) {
            String keys = "key" + i;
            Message message = new Message(TEST_QUICK_TOPIC, "top A", keys, ("发送消息xxx:" + i).getBytes());

            /// RemotingException, InterruptedException, MQBrokerException
            SendResult result = producer.send(message);
            System.out.println(result);
        }

        /// 发送完消息,关闭producer
        producer.shutdown();
    }
}
